#ifndef MEM_INCLUDED
#define MEM_INCLUDED
#include "except.h"
extern const Except_T Mem_Failed;

/**
 * @brief alloc a n bytes memory
 *
 * @param nbytes
 * @param file  __FILE__
 * @param line  __LINE__
 * @return void* the point to the alloced memory
 */
extern void *Mem_alloc(long nbytes,
                       const char *file, int line);

/**
 * @brief alloc n count elements, every element is n bytes long;
 * then init memory to 0
 *
 * @param count
 * @param nbytes
 * @param file
 * @param line
 * @return void*
 */
extern void *Mem_calloc(long count, long nbytes,
                        const char *file, int line);

/**
 * @brief free thr ptr memory
 *
 * @param ptr
 * @param file
 * @param line
 */
extern void Mem_free(void *ptr,
                     const char *file, int line);
extern void *Mem_resize(void *ptr, long nbytes,
                        const char *file, int line);
#define ALLOC(nbytes) \
    Mem_alloc((nbytes), __FILE__, __LINE__)
#define CALLOC(count, nbytes) \
    Mem_calloc((count), (nbytes), __FILE__, __LINE__)
#define NEW(p) ((p) = ALLOC((long)sizeof *(p)))
#define NEW0(p) ((p) = CALLOC(1, (long)sizeof *(p)))
#define FREE(ptr) ((void)(Mem_free((ptr),               \
                                   __FILE__, __LINE__), \
                          (ptr) = 0))
#define RESIZE(ptr, nbytes) ((ptr) = Mem_resize((ptr), \
                                                (nbytes), __FILE__, __LINE__))
#endif